<template>
  <div class="goodsinfo-container">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="flag"></div>
    </transition>
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotu="lunbotuList" :imgname="'img'" :isfull="true"></swiper>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-header">{{goodlists.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <span style="margin-right:10px">
              <del>￥{{goodlists.market_price}}</del>
            </span>
            销售价：
            <span
              style="font-size:16px;color:red;font-weight:bold;"
            >￥{{goodlists.sell_price}}</span>
          </p>
          <p>
            购买数量:
            <!-- 子组件向父组件传值   @func="getSelectedCount"   -->
            <nobox :num="goodlists.stock_quantity" @func="getSelectedCount"></nobox>
          </p>

          <div>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small"  @click="addToCart">加入购物车</mt-button>
          </div>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodlists.goods_no}}</p>
          <p>库存情况：{{goodlists.stock_quantity}}件</p>
          <p>上架时间：{{goodlists.add_time|dateFormate}}</p>
        </div>
      </div>
      <div class="mui-card-footer bntarea">
        <mt-button type="primary" size="large" plain @click="goDesc">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swiper from "../sub-components/Swiper.vue";
import nobox from "../sub-components/goodsinfo_nobox.vue";
export default {
  data() {
    return {
      lunbotuList: [],
      goodlists: [],
      flag: false,
      SelectedCount:1,
    };
  },
  created() {
    this.getLunbotu();
    this.getGoodsList();
  },
  methods: {
    getLunbotu() {
      // 获取轮播图数据的方法
      this.$http.get("/api/getlunbo").then(result => {
        if (result.data.status === 0) {
          // 成功了
          this.lunbotuList = result.data.message;
        } else {
          // 失败的
        }
      });
    },
    getGoodsList() {
      this.$http.get("/api/goods/getinfo/" + this.id).then(result => {
        this.goodlists = result.data.message[0];
        console.log(this.goodlists);
      });
    },
    goDesc() {
      //可以传递参数
      this.$router.push("/home/goodsDesc/" + this.goodlists.id);
    },
    goComment() {
      //  this.$router.push("/home/goodsComment/" +this.goodlists.id);
      this.$router.push({ name: "cmt", params: { id: this.id } });
    },
    addToCart() {
      this.flag = !this.flag
      console.log("SelectedCount= "+this.SelectedCount)
      this.$store.commit("addToCar",{id:this.id,count:this.SelectedCount,selected:true,price:this.goodlists.sell_price})
    },
   
    beforeEnter(el){
      //入场动画开始之前，设置小球的起始状态
      el.style.transform="translate(0,0)";
    },
    enter(el,done){
      //入场动画开始之后，设置小球的终止状态
      //transform : 变化
      //translate：位移
      //transition:过渡效果
      el.offsetWidth;

        //小球动画兼容性
      const ballPos=el.getBoundingClientRect();
      const badgePos=document.getElementById("badge").getBoundingClientRect();
      const left=badgePos.left-ballPos.left;
      const top=badgePos.top-ballPos.top;

      el.style.transform="translate("+left+"px,"+top+"px)";
      el.style.transition="all 1s cubic-bezier(.54,-0.44,.62,.66)";
      done()

    },
    afterEnter(el ){
      //入场动画完成之后的回调函数
      this.flag=!this.flag
    },
    //
    getSelectedCount(c){
      //购物车上的值
      this.SelectedCount=c;
      console.log("子组件向父组件传递的值"+c+"  SelectedCount=  "+this.SelectedCount)
      
    }
  },

  props: ["id"],
  components: {
    swiper,
    nobox
  }
};
</script>
<style lang="scss" scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;
}
.bntarea {
  flex-direction: column;
  button + button {
    margin-top: 15px;
  }
}
.ball {
  width: 15px;
  height: 15px;
  background-color: red;
  border-radius: 50%;
  position: absolute;
  z-index: 99;
  left: 140px;
  top: 410px;
  // transform: translate(95px,390px);
}
</style>